package com.translate.web.service.impl;

import com.translate.web.dao.OrderLogMapper;
import com.translate.web.param.contract.IIdAndUserIdParam;
import com.translate.web.param.contract.IPayBehaviorRequestParam;
import com.translate.web.param.contract.ISecondaryServiceParam;
import com.common.util.CopyEntity;
import com.translate.entity.OrderLog;
import com.translate.web.service.IOrderLogService;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
public class OrderLogServiceImpl extends BaseTranslateServiceImpl<OrderLog> implements IOrderLogService {
	public OrderLogServiceImpl(OrderLogMapper mapper) {
		super(mapper);
	}

	protected void insertPropteris(OrderLog data) {
		long time = System.currentTimeMillis() / 1000;
		data.setCreateTime(time);
	}

	protected Class<OrderLog> getEntityClass() {
		return OrderLog.class;
	}
	
	// 0 初始 1 反馈 2 售后3 新增
	private static Map<Integer, Integer> ORDER_LOG_STATUS = new HashMap<Integer, Integer>(); 
	 
	static {
		// 反馈,
		ORDER_LOG_STATUS.put(1, 11);
		
		// 售后,
		ORDER_LOG_STATUS.put(2, 10);
		
		ORDER_LOG_STATUS.put(13, 13);
		
		ORDER_LOG_STATUS.put(12, 12);
	};
	
	

	@Transactional(propagation = Propagation.MANDATORY)
	@Override
	public int paySuccessOrderLog(int id, int userId, int[] orderIds) {

		return paySuccessOrderLog(id, userId, orderIds, (byte) 14);

	}
	
	@Transactional(propagation = Propagation.MANDATORY)
	@Override
	public int paySuccessOrderLog(int id, int userId, int[] orderIds, byte status) {

		OrderLog[] orderLog = gerenateOrderLog(
				id,userId, orderIds,
				status, 
				"", 
				(byte) 2);


		return this.batchInsert(orderLog);

	}
	

	private OrderLog[] gerenateOrderLog(
			int id, int userId, 
			int[] orderIds, 
			byte orderStatus, 
			String requirement,
			byte type
	) {
		OrderLog[] orderLog = new OrderLog[orderIds.length];

		long time = System.currentTimeMillis() / 1000;

		for (int i = 0; i < orderIds.length; i++) {
			orderLog[i] = new OrderLog();
			orderLog[i].setOrderId(orderIds[i]);
            orderLog[i].setUserId(userId);
            orderLog[i].setPayManagerId(id);
            orderLog[i].setCreateTime(time);
            orderLog[i].setType(type);
            orderLog[i].setRequirement(requirement);
            orderLog[i].setOrderStatus(orderStatus);
		}
		return orderLog;
	}

	@Override
	public int toBeConfirmed(IPayBehaviorRequestParam requestParam, int[] orderIds) {
		return toBeConfirmed(requestParam, orderIds, (byte) 3);
	}
	
	@Override
	public int toBeConfirmed(IPayBehaviorRequestParam requestParam, int[] orderIds, byte orderStatus) {
		OrderLog[] orderLog = gerenateOrderLog(requestParam.getId(), (int)requestParam.getUserId(), orderIds, orderStatus, "", (byte) 2);
		return this.batchInsert(orderLog);
	}
	
	@Override
	@Transactional(propagation = Propagation.MANDATORY)
	public int drawOrderLog(IPayBehaviorRequestParam requestParam) {
		
		OrderLog[] orderLog = gerenateOrderLog(requestParam.getId(), (int)requestParam.getUserId(), new int[] {requestParam.getId()}, (byte) 2, "待支付", (byte) 2);
		
		return this.batchInsert(orderLog);
	}
	
	@Resource
	public void setRedisTemplate(RedisTemplate<String, ? extends Object> redisTemplate) {
		this.redisTemplate = redisTemplate;
	}



	@Override
	public <M> int secondaryServiceCancelLog(
			ISecondaryServiceParam<M> service,
			int payManagerId
	) {

		OrderLog orderLog = new OrderLog();

		orderLog.setOrderId(service.getOrderId());

		orderLog.setRequirement(service.getRequirement());

		orderLog.setUserId((int) service.getUserId());

		orderLog.setPayManagerId(payManagerId);
		
		orderLog.setType((byte) 2);

		Integer integer = ORDER_LOG_STATUS.get(service.getServiceStatus().intValue());
		
		if (integer == null) {
			throw new IllegalArgumentException("找不到这个二次服务");
		}
		
		orderLog.setOrderStatus(integer.byteValue());
		
		this.insertPropteris(orderLog);
		
		this.insert(orderLog);
		
		int id = orderLog.getId();
		
		
		return id;
	}
	
	

	
	@Override
	public <M> int secondaryService(
			ISecondaryServiceParam<M> service,
			int payManagerId
	) {

		OrderLog orderLog = new OrderLog();

		orderLog.setOrderId(service.getOrderId());

		orderLog.setRequirement(service.getRequirement());

		orderLog.setUserId((int) service.getUserId());

		orderLog.setPayManagerId(payManagerId);
		
		orderLog.setType((byte) 2);

		Integer integer = ORDER_LOG_STATUS.get(service.getServiceStatus().intValue());
		
		if (integer == null) {
			throw new IllegalArgumentException("找不到这个二次服务");
		}
		
		orderLog.setOrderStatus(integer.byteValue());
		
		this.insertPropteris(orderLog);
		
		this.insert(orderLog);
		
		int id = orderLog.getId();
		
		// 待审核 
		orderLog = new OrderLog();

		orderLog.setOrderId(service.getOrderId());

		orderLog.setRequirement(service.getRequirement());

		orderLog.setUserId((int) service.getUserId());

		orderLog.setPayManagerId(payManagerId);
		
		orderLog.setType((byte) 2);

		orderLog.setOrderStatus((byte) 6);

		long timeMillis = (System.currentTimeMillis() +1000L) / 1000L;

		orderLog.setCreateTime(timeMillis);
		
		this.mapper.insert(orderLog);
		
		return id;
	}
	

	@Override
	@Transactional(propagation = Propagation.MANDATORY)
	public OrderLog selectSecondaryService(IIdAndUserIdParam userIdParam, int orderStatus) {

		OrderLogMapper mapper = this.getBaseMapper();

		return mapper.selectSecondaryService(userIdParam.getId(), (int) userIdParam.getUserId() ,orderStatus);

	}

	@Override
	public int commentOrderLog(IIdAndUserIdParam requestParam, int payManagerId) {
		
		OrderLog orderLog = new OrderLog();

		orderLog.setOrderId(requestParam.getId());


		orderLog.setUserId((int) requestParam.getUserId());

		orderLog.setPayManagerId(payManagerId);
		
		orderLog.setType((byte) 2);

		orderLog.setOrderStatus((byte) 20);
		
		this.insertPropteris(orderLog);
		
		this.insert(orderLog);
		return orderLog.getId();
		
	}

	@Override
	public int assocIdOrderLog(int id, int userId, int[] orderIds) {
		OrderLog[] orderLog = gerenateOrderLog(
				id,userId, orderIds,
				(byte) 23, 
				"", 
				(byte) 2);
		
		return this.batchInsert(orderLog);

	}
}
